package com.zy.controller;

import com.zy.mapper.CarouselMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author LittleDesire
 * @description
 * @date 2022-04-03 周日 16:41
 * @since 17
 */
@RestController
public class CarouselController {
    @Autowired(required = false)
    private CarouselMapper cm;

    @PutMapping("updateisshow")
    public Map<String, Object> updateIsShow(@RequestBody Map<String, Object> mm) {
        int v = Integer.parseInt(mm.get("v").toString());
        int id = Integer.parseInt(mm.get("id").toString());
        cm.updateIsShow(v, id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "\"" + mm.get("t") + "\": " + (v == 0 ? "隐藏" : "显示"));
        return map;
    }

    @DeleteMapping("delete")
    public Map<String, Object> deleteById(@RequestBody Map<String, Object> mm, HttpServletRequest req) {
        int id = Integer.parseInt(mm.get("id").toString());
        cm.deleteById(id);
        //删除文件
        String src = req.getServletContext().getRealPath(mm.get("src").toString());
        File file = new File(src);
        if (file.exists()) {
            file.delete();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "\"" + mm.get("t") + "\": 已删除");
        return map;
    }

    @DeleteMapping("/cal/delall")
    public Map<String, Object> delall(@RequestBody List<Map<String, Object>> list,HttpServletRequest req) {

        for (Map<String, Object> m : list) {
            int id = Integer.parseInt(m.get("id").toString());
            cm.deleteById(id);
            //删除文件
            String src = req.getServletContext().getRealPath("/static/"+m.get("src").toString());
            File file = new File(src);
            if (file.exists()) {
                file.delete();
            }
        }
        //返回信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "批量删除成功");
        return map;
    }


    @GetMapping("/cal/add")

    public ModelAndView add() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("cal/add");
        return mv;
    }


    @GetMapping("/cal/edit")
    public ModelAndView edit() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("cal/edit");
        return mv;
    }


    @PostMapping("/cal/save")
    public Map<String, Object> save(MultipartFile file, String title, int isshow, String link, HttpServletRequest req) throws IOException {
        //处理上传文件
        String filename = Objects.requireNonNull(file.getOriginalFilename()).toLowerCase();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date());
        String ext = filename.substring(filename.lastIndexOf("."));
        filename = date+ext;
        if (!file.isEmpty()) {
            String path = req.getServletContext().getRealPath("/static/carousel/");
            File ff = new File(path);
            if (!ff.exists()) ff.mkdirs();
            file.transferTo(new File(path, filename));
        }
        //保存数据到mysql数据库
        Map<String, Object> v = new HashMap<>();
        v.put("link", link);
        v.put("title", title);
        v.put("isshow", isshow);
        v.put("src", "carousel/" + filename);
        cm.save(v);

        //返回信息
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 200);
        map.put("msg", "轮播添加成功");
        return map;
    }
}
